/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.DAO.mysqlDAO;

import assignment_cp3120.DAO.DepartureLocationDAO;
import assignment_cp3120.dataObjects.DepartureLocation;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author Richard Irons
 */
public class MysqlDepartureLocatonDAO implements DepartureLocationDAO{
    public Vector<DepartureLocation> getDepLocations() {
        Vector<DepartureLocation> depLoc = new Vector<DepartureLocation>();
        try{            
            Connection c = MysqlDA0Factory.createConnection();
            Statement statement = c.createStatement();
            ResultSet rs = statement.executeQuery(
                    "SELECT * FROM DEPARTURE_LOCATION");
            if(rs.isBeforeFirst()){
                rs.next();
            }
            if(rs.first()){
                do {
                    DepartureLocation tempDepLoc = new DepartureLocation();
                    tempDepLoc.setDep_id(rs.getString("DEP_ID"));
                    tempDepLoc.setDep_name(rs.getString("DEP_NAME"));
                    tempDepLoc.setAirport_tax(rs.getInt("AIRPORT_TAX"));
                    depLoc.add(tempDepLoc);
                }
                while(rs.next());
            }
            c.close();
        }catch(SQLException e){System.out.println(e.getMessage());}        
        return depLoc;
    }
    public int getAirportTax() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public String[] getDepNames() {
        Vector<String> depLocArray = new Vector<String>();
        try{            
            Connection c = MysqlDA0Factory.createConnection();
            Statement statement = c.createStatement();
            ResultSet rs = statement.executeQuery(
                    "SELECT DISTINCT DEP_NAME FROM DEPARTURE_LOCATION");
            if(rs.isBeforeFirst()){
                rs.next();
            }            
            if(rs.first()){
                while(rs.next()){
                    depLocArray.add(rs.getString("DEP_NAME"));
                }
            }            
            c.close();
        }catch(SQLException e){System.out.println(e.getMessage());}        
        String[] depLoc = new String[depLocArray.size()];
        for (int i = 0; i < depLocArray.size(); i++) {
            depLoc[i] = depLocArray.get(i);
        }
        return depLoc;
    }

    public int createAirport(DepartureLocation airport) {
        Connection c = MysqlDA0Factory.createConnection();
        int result = 0;
        try{
            Statement statment = c.createStatement();
            result = statment.executeUpdate(
                    "INSERT INTO 3120S109g09.DEPARTURE_LOCATION ( "
                    + "DEP_ID, "
                    + "DEP_NAME, "
                    + "AIRPORT_TAX "
                    + ") VALUES ( "
                    + "\"" + airport.getDep_id() + "\", "
                    + "\"" + airport.getDep_name() + "\", "
                    + "\"" + airport.getAirport_tax() + "\" "
                    + ")");

        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
        return result;
    }

    public int deleteAirport(String id) {
        Connection c = MysqlDA0Factory.createConnection();
        int result = 0;
        try{
            Statement statment = c.createStatement();
            result = statment.executeUpdate(
                    "DELETE FROM `DEPARTURE_LOCATION` WHERE DEP_ID = \""
                    +id
                    +"\" LIMIT 1 ");

        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
        return result;
    }
}